﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CF_DAL_WS.Models;
using CF_DAL_WS.Helpers;
using System.Web.Security;

namespace CF_DAL_WS.DTO
{
    public class UsersDTO
    {
        public Guid ID { get; set; }
        public RoleDTO Role { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public bool IsLockedOut { get; set; }
        public string LastLoginDate { get; set; }
        public List<OrdersDTO> Orders { get; set; }

        public static UsersDTO Translate(Users Entity)
        {
            if (Entity != null)
            {
                var dto = new UsersDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    Phone = Entity.Phone,
                    Address = Entity.Address,
                    UserName = Entity.aspnet_Users.UserName,
                    Email = Entity.aspnet_Users.aspnet_Membership.Email,
                    IsLockedOut = Entity.aspnet_Users.aspnet_Membership.IsLockedOut,
                    LastLoginDate = Entity.aspnet_Users.aspnet_Membership.LastLoginDate.ToShortDateString(),
                    Orders = new List<OrdersDTO>()
                };
                var roleEntity = Entity.aspnet_Users.aspnet_Roles.FirstOrDefault();
                dto.Role = new RoleDTO
                {
                    ID = roleEntity.RoleId,
                    Name = roleEntity.RoleName
                };
                if (Entity.Orders != null)
                {
                    foreach (var item in Entity.Orders)
                    {
                        dto.Orders.Add(OrdersDTO.Translate(item));
                    }
                }
                return dto;
            }
            return null;
        }

        public static UsersDTO TranslateBase(Users Entity)
        {
            try
            {
                if (Entity != null)
                {
                    var dto = new UsersDTO
                    {
                        ID = Entity.ID,
                        Name = Entity.Name,
                        Phone = Entity.Phone,
                        Address = Entity.Address,
                        UserName = Entity.aspnet_Users.UserName,
                        Email = Entity.aspnet_Users.aspnet_Membership.Email,
                        IsLockedOut = Entity.aspnet_Users.aspnet_Membership.IsLockedOut,
                        LastLoginDate = Entity.aspnet_Users.aspnet_Membership.LastLoginDate.ToShortDateString()
                    };
                    var roleEntity = Entity.aspnet_Users.aspnet_Roles.FirstOrDefault();
                    dto.Role = new RoleDTO
                    {
                        ID = roleEntity.RoleId,
                        Name = roleEntity.RoleName
                    };
                    return dto;
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public static Users Reverse(UsersDTO obj)
        {
            var entity = new Users
            {
                Name = obj.Name,
                Phone = obj.Phone,
                Address = obj.Address
            };
            entity.aspnet_Users.UserName = obj.UserName;
            entity.aspnet_Users.aspnet_Membership.Password = obj.Password;
            entity.aspnet_Users.aspnet_Membership.Email = obj.Email;
            return entity;
        }

        public void UpdateEntity(ref Users entity)
        {
            entity.Name = Name;
            entity.Phone = Phone;
            entity.Address = Address;
            entity.aspnet_Users.aspnet_Membership.IsLockedOut = IsLockedOut;
        }
    }
}